import { StatusRun } from '../status/runStatus'
const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    scoreLabel:cc.Label=null

    score:number

    status: StatusRun
    onLoad() {
        this.status = StatusRun.run
        this.score=0;
    }

    // update(dt: number) {

    // }

    //跳跃
    jump() {
        let rigbody=this.node.getComponent(cc.RigidBody);
        switch (this.status) {
            case StatusRun.run:
                
                rigbody.linearVelocity=cc.v2(0,640);
                this.statusChange(StatusRun.jump)
                break;
            case StatusRun.jump:
                //rigbody.linearVelocity=cc.v2(0,600);
                this.statusChange(StatusRun.double_jump)
                break;
        }
    }

    //二段跳
    double_jump(){
        
    }

    //落地
    floor() {
        switch (this.status) {
            case StatusRun.jump:
                this.statusChange(StatusRun.run);
                break
        }
    }

    addScore(){
        this.score++;
        this.scoreLabel.string=this.score.toString();
    }

    statusChange(status: StatusRun) {
        if (this.status != status) {
            this.status = status;
            let animation = this.node.getComponent(cc.Animation);
            animation.play(status);
        }
    }

    onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
       
        if(self.node.y<other.node.y){
            contact.disabled=true;
        }
        if (other.node.group == 'platform') {
            this.floor();
        }  
        if(other.node.group == 'gold'){
            other.node.destroy();
            this.addScore();
            
        }
        
        
    }
		
}
